package com.bestcem.xm.member.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.common.core.uitls.StringUtil;
import com.bestcem.xm.member.dao.ExtendFieldEnumItemDao;
import com.bestcem.xm.member.dao.mapper.ExtendFieldEnumItemMapper;
import com.bestcem.xm.member.entity.mongo.ExtendFieldEnumItem;
import com.bestcem.xm.member.entity.mysql.ExtendFieldEnumItemEntity;
import com.bestcem.xm.member.entity.pojo.ExtendFieldEnumItemDO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author dc.wang <dc.wang@idiaoyan.com>
 * @version V1.0
 * @description
 * @date 2021/7/21
 */
@Repository
public class ExtendFieldEnumItemDaoImpl implements ExtendFieldEnumItemDao {

    @Autowired
    private ExtendFieldEnumItemMapper extendFieldEnumItemMapper;

    @Override
    public Integer batchDeleteByIds(@NotNull Collection<String> ids) {
        return extendFieldEnumItemMapper.deleteByIds(ids);
    }

    @Override
    public Integer batchDeleteByFieldIds(@NotNull Collection<String> fieldIds) {
        return extendFieldEnumItemMapper.deleteByFieldIds(fieldIds);
    }

    @Override
    public List<ExtendFieldEnumItemDO> findByFieldIdsAndDisplayStatus(Collection<String> ids, int displayStatus) {
        List<ExtendFieldEnumItemEntity> extendFieldEnumItemEntityList = extendFieldEnumItemMapper.findDisplayEnumsByFieldIds(ids, displayStatus);
        if (CollUtil.isNotEmpty(extendFieldEnumItemEntityList)) {
            return listToDO(extendFieldEnumItemEntityList);
        }
        return new ArrayList<>();
    }

    @Override
    public void batchUpdate(@NotEmpty Collection<ExtendFieldEnumItemDO> enumItems) {
        Date updateTime = DateUtil.getCommonDate();
        for (ExtendFieldEnumItemDO enumItem : enumItems) {
            enumItem.setUpdateTime(updateTime);
            extendFieldEnumItemMapper.updateItem(toEntity(enumItem));
        }
    }

    @Override
    public Collection<ExtendFieldEnumItemDO> findByFieldIds(@NotEmpty Collection<String> extendFields) {
        if(CollectionUtils.isEmpty(extendFields)){
            return new ArrayList<>();
        }
        List<ExtendFieldEnumItemEntity> extendFieldEnumItemEntityList = extendFieldEnumItemMapper.listByFieldIds(extendFields);
        return listToDO(extendFieldEnumItemEntityList);
    }

    @Override
    public List<ExtendFieldEnumItemDO> findByFieldIds(List<String> extendFieldIds, boolean isDisplay) {
        // Query query = Query.query(criteria).with(Sort.by(Sort.Order.asc(ColumnConstant.ExtendFieldEnumItem.SERIAL)));
        return findByFieldIdsAndDisplayStatus(extendFieldIds, isDisplay ? 1 : 0);
    }

    @Override
    public Integer batchInsert(@NotEmpty Collection<ExtendFieldEnumItemDO> enumItems) {
        return extendFieldEnumItemMapper.batchInsert(listToEntity(new ArrayList<>(enumItems)));
    }

    @Override
    public List<ExtendFieldEnumItemDO> findEnumsByIds(Collection<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new ArrayList<>(0);
        }
        List<ExtendFieldEnumItemEntity> extendFieldEnumItemEntityList = extendFieldEnumItemMapper.listByIds(ids);
        return listToDO(extendFieldEnumItemEntityList);
    }

    /**
     * list : entity -> DO
     *
     * @param extendFieldEnumItemEntityList entityList
     * @return DOList
     */
    private List<ExtendFieldEnumItemDO> listToDO(List<ExtendFieldEnumItemEntity> extendFieldEnumItemEntityList) {
        return extendFieldEnumItemEntityList.stream().map(this::toDO).collect(Collectors.toList());
    }

    /**
     * entity -> DO
     *
     * @param extendFieldEnumItemEntity entity
     * @return DO
     */
    private ExtendFieldEnumItemDO toDO(ExtendFieldEnumItemEntity extendFieldEnumItemEntity) {
        ExtendFieldEnumItemDO extendFieldEnumItemDO = new ExtendFieldEnumItemDO();
        BeanUtils.copyProperties(extendFieldEnumItemEntity, extendFieldEnumItemDO);
        return extendFieldEnumItemDO;
    }

    /**
     * list : DO -> entity
     *
     * @param extendFieldEnumItemDOList DOList
     * @return entityList
     */
    private List<ExtendFieldEnumItemEntity> listToEntity(List<ExtendFieldEnumItemDO> extendFieldEnumItemDOList) {
        return extendFieldEnumItemDOList.stream().map(this::toEntity).collect(Collectors.toList());
    }

    /**
     * entity -> DO
     *
     * @param extendFieldEnumItemDO DO
     * @return entity
     */
    private ExtendFieldEnumItemEntity toEntity(ExtendFieldEnumItemDO extendFieldEnumItemDO) {
        if (StrUtil.isEmpty(extendFieldEnumItemDO.getFieldEnumId())) {
            extendFieldEnumItemDO.setFieldEnumId(StringUtil.getId());
        }
        ExtendFieldEnumItemEntity extendFieldEnumItemEntity = new ExtendFieldEnumItemEntity();
        BeanUtils.copyProperties(extendFieldEnumItemDO, extendFieldEnumItemEntity);
        return extendFieldEnumItemEntity;
    }

    private ExtendFieldEnumItemEntity convert(ExtendFieldEnumItem enumItem){
        ExtendFieldEnumItemEntity entity = new ExtendFieldEnumItemEntity();
        entity.setFieldEnumId(enumItem.getId());
        if(enumItem.getExtendFieldId() != null){
            entity.setExtendFieldId(enumItem.getExtendFieldId().toHexString());
        }
        entity.setName(enumItem.getName());
        entity.setSeq(enumItem.getSerial());
        entity.setDisplayStatus(enumItem.getDisplayStatus());
        entity.setDeleteStatus(enumItem.getDisplayStatus());
        entity.setCreateTime(enumItem.getCreateTime());
        entity.setUpdateTime(enumItem.getUpdateTime());
        return entity;
    }
}
